// $(function () {
// findAll();
//
// })
layui.use(['form','laypage', 'layer','laydate'], function () {
    var laypage = layui.laypage,
         form = layui.form
        , layer = layui.layer,
        laydate = layui.laydate;
    //执行一个laydate实例
    laydate.render({
        elem: '#start' //指定元素
        , trigger: 'click'
    });
    laydate.render({
        elem: '#end' //指定元素
        , trigger: 'click'
    });

    // let res = myAjax("/back/user/getCount", {});
    //执行一个laydate实例
    laydate.render({
        elem: '#birthday' //指定元素
        , trigger: 'click'
    });
    //监听提交
    form.on('submit(sreach)',
        function(data) {
        data=data.field;
            let arr_box = [];
            $('input[type=checkbox][name=hobby]:checked').each(function () {
                arr_box.push($(this).attr("title"));
            });
            console.log(data);
            data.hobby=arr_box.toLocaleString()
            page(data);
            return false;
        });
    page({});
});
function page(data) {
    layui.use(['laypage'], function () {
        var laypage = layui.laypage;


        //完整功能
         laypage.render({
             elem: 'page'
             , count: getCount(data)
             , layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
             , jump: function (obj) {

                 // let data = {page: obj.curr, limit: obj.limit};
                // console.log(data)
                findAll(obj.curr,obj.limit,data);
            }
         });
    });
}
function getCount(data){
    let count=0;
    $.ajax({
        url:'/back/user/getCount',
        type:'get',
        data:data,
        async:false,
        dataType:'json',
        success:function (res) {
          count= res.count;
        }
    })
    return count;
    
}
function findAll(curr,limit,data) {

    let page={pageNo:curr,pageSize:limit}
    $.ajax({
        url:'/back/user/findAll',
        data:Object.assign(data,page),
        type:'get',
        dataType:'json',
        success: function (res) {
            console.log(res)
            let  data=res.data
            let html='';
            for (let i = 0; i <data.length; i++) {
                html+='<tr>\n' +
                    '                                    <td>\n' +
                    '                                      <input type="checkbox" name="id" value="1"   lay-skin="primary"> \n' +
                    '                                    </td>\n' +
                    '                                    <td>'+data[i].id+'</td>\n' +
                    '                                    <td>'+data[i].username+'</td>\n' +
                    '                                    <td>'+data[i].sex+'</td>\n' +
                    '                                    <td>'+data[i].phone+'</td>\n' +
                    '                                    <td>'+data[i].adress+'</td>\n' +
                    '                                    <td>'+data[i].hobby+'</td>\n' +
                    '                                    <td>'+data[i].birthday+'</td>\n' +
                    '                                    <td class="td-status">\n' +
                    '                                      <span class="layui-btn layui-btn-normal layui-btn-mini '+(data[i].enable==1?'':'layui-btn-disabled')+' ">'+(data[i].enable ==1? '启用':'未启用')+'</span></td>\n' +
                    '                                    <td class="td-manage">\n' +
                    '                                      <a onclick="member_stop(this,'+data[i].id+','+data[i].enable+')" href="javascript:;"  title="'+(data[i].enable ==1? '启用':'未启用')+'">\n' +
                    '                                        <i class="layui-icon">'+(data[i].enable==1?'&#xe601;':'&#xe62f')+'</i>\n' +
                    '                                      </a>\n' +
                    '                                      <a title="编辑"  onclick="edit('+data[i].id+')" href="javascript:;">\n' +
                    '                                        <i class="layui-icon">&#xe642;</i>\n' +
                    '                                      </a>\n' +
                    '                                      <a onclick="editPassword(\''+data[i].username+'\','+data[i].id+')" title="修改密码" href="javascript:;">\n' +
                    '                                        <i class="layui-icon">&#xe631;</i>\n' +
                    '                                      </a>\n' +
                    '                                      <a title="删除" onclick="del(this, '+data[i].id+')" href="javascript:;">\n' +
                    '                                        <i class="layui-icon">&#xe640;</i>\n' +
                    '                                      </a>\n' +
                    '                                    </td>\n' +
                    '                                  </tr>';
            }
            $("#tbody").html(html);
        }
    })

}
function edit(id) {
    console.log(id)
    sessionStorage.setItem("userId",id);
    xadmin.open('编辑','member-edit.html',600,400)
}
function del(obj,id) {
    layer.confirm('确认要删除吗？',function(index){
        $.ajax({
            url:'/back/user/delete',
            data:{id:id},
            type:'post',
            dataType:'json',
            success: function (data) {
                if (data.msg == 'success') {
                    $(data).parents("tr").remove();
                    layer.msg("已删除",{icon:1,time:1000});

                } else {
                    layer.alert("删除失败");
                    console.log(data.id)
                }
            }
        })
    });

}
function editPassword(username,id) {
    sessionStorage.setItem("username",username);
    sessionStorage.setItem("userId",id)
    xadmin.open('修改密码','member-password.html',600,400)
}
layui.use(['laydate','form'], function(){
    var laydate = layui.laydate;
    var  form = layui.form;



});

/*用户-停用*/
function member_stop(obj,id,enable){
    layer.confirm('确认要'+(enable==1?'停用':'启用')+'吗？',function(index){
        $.ajax({
            url:'/back/user/edit',
            data:{id:id,enable :enable==1?0:1},
            dataType:'json',
            type:'post',
            success:function (data) {

                if (data.msg == 'success') {
                    if($(obj).attr('title')=='启用'){

                        //发异步把用户状态进行更改
                        $(obj).attr('title','停用')
                        $(obj).find('i').html('&#xe62f;');

                        $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');
                        layer.msg('已停用!',{icon: 5,time:1000});

                    }else{
                        $(obj).attr('title','启用')
                        $(obj).find('i').html('&#xe601;');

                        $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');
                        layer.msg('已启用!',{icon: 6,time:1000});
                    }
                }else {
                    layer.msg('启用失败!',{icon: 5,time:1000});
                }

            }


        })


    });
}




